/*
 * Copyright (c) 2011, Yawning <yawninglol at gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   * Neither the name of the Mew Developers nor the
 *     names of its contributors may be used to endorse or promote products
 *     derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

package yawning.mew.sim;

public class SimulationEventTargetCast extends SimulationEvent {
	SimulationTarget mTarget;
	double mTargetCastInterval;
	double mTargetCastTime;

	boolean mIsCasting = false;
	boolean mWasInterrupted = false;
	double mNextCastAt;
	private boolean mIsStopping = false;

	int mNrCasts;
	int mNrInterruptedCasts;

	boolean mIsLogging;
	SimulationLog mLog;

	public SimulationEventTargetCast(SimulationEngine su, SimulationTarget target) {
		super(su);

		mTarget = target;

		mIsLogging = mSimEngine.isLogging();
		mLog = mSimEngine.getLog();

		mTargetCastInterval = target.getTarget().getCastInterval();
		mTargetCastTime = target.getTarget().getCastTime();

		// Start the mob cast timer.
		// Note: I give the script a grace period of 1xTargetCastInterval before starting my first cast.
		if (mTargetCastInterval > 0) {
			schedule(mTargetCastInterval);
			mNextCastAt = mSimEngine.getSimTime() + mTargetCastInterval;
		}
	}

	@Override
	public void onExpire() {
		if (!mIsStopping) {
			if (mIsCasting == false) {
				mWasInterrupted = false;
				mIsCasting = true;

				mNrCasts++;

				mNextCastAt = mSimEngine.getSimTime() + mTargetCastInterval;

				if (mIsLogging) mLog.log("Target Cast: %s Start\n", mTarget.getTarget().getName());

				onCastStart();

				schedule(mTargetCastTime);
			} else {
				mIsCasting = false;

				if (mWasInterrupted == false) {
					if (mIsLogging) mLog.log("Target Cast: %s Complete\n", mTarget.getTarget().getName());

					onCastStop();
				}

				schedule(mNextCastAt - mSimEngine.getSimTime());
			}
		}
		mIsStopping = false;
	}

	public void stop() {
		if (isUp()) { mIsStopping = true; }
		cancel();
	}

	public void restart() {
		mIsCasting = false;
		mWasInterrupted = false;

		if (mTargetCastInterval > 0) {
			schedule(mTargetCastInterval);
			mNextCastAt = mSimEngine.getSimTime() + mTargetCastInterval;
		}
	}

	public void interrupt() {
		if (mIsCasting) {
			if (!mWasInterrupted) {
				mNrInterruptedCasts++;

				if (mIsLogging) mLog.log("Target Cast: %s Interrupted\n",  mTarget.getTarget().getName());

				onCastStop();
			}

			mWasInterrupted = true;
		}
	}

	public void stun(double duration) {
		if (isUp()) {
			if (mIsCasting) {
				if (!mWasInterrupted) {
					// Ok, you stunned the mob mid-cast.  Treat it like an interrupt, but schedule the next cast quickly.
					mNrInterruptedCasts++;

					if (mIsLogging) mLog.log("Target Cast: %s Stunned\n", mTarget.getTarget().getName());

					// This restarts the swing timers and other things even though the stun code is just going to stop them again.
					onCastStop();

					mIsCasting = false;
					mWasInterrupted = false;

					// Stunning here just pushes the next cast back by the duration of the stun (Mob dependent?).
					mNextCastAt = mSimEngine.getSimTime() + duration;
					schedule(duration);
				} else {
					// Stun after interrupt, but within the interrupt window, just treat it identically to the out of casting case.
					mNextCastAt = Math.max(mSimEngine.getSimTime() + duration, mNextCastAt);
					schedule(mNextCastAt - mSimEngine.getSimTime());
				}
			} else {
				// Not casting at all.
				// XXX: I assume that the mob's cast cooldown ticks while the mob is stunned, so just prevent the mob from casting during the stun.
				mNextCastAt = Math.max(mSimEngine.getSimTime() + duration, mNextCastAt);
				schedule(mNextCastAt - mSimEngine.getSimTime());
			}
		}
	}

	private boolean mSwingWasEnabled;
	private boolean mYellowWasEnabled;
	private boolean mMagicWasEnabled;

	private void onCastStart() {
		if (mTarget.getTargetSwingEv().isUp()) {
			mSwingWasEnabled = true;
			mTarget.getTargetSwingEv().stop();
		}

		if (mTarget.getTargetYellowEv().isUp()) {
			mYellowWasEnabled = true;
			mTarget.getTargetYellowEv().pause();
		}

		if (mTarget.getTargetMagicEv().isUp()) {
			mMagicWasEnabled = true;
			mTarget.getTargetMagicEv().pause();
		}
	}

	private void onCastStop() {
		if (mSwingWasEnabled == true) mTarget.getTargetSwingEv().start();
		if (mYellowWasEnabled == true) mTarget.getTargetYellowEv().start();
		if (mMagicWasEnabled == true) mTarget.getTargetMagicEv().start();
	}

	public boolean isCasting() {
		return mIsCasting && !mWasInterrupted;
	}

	public double nextCastIn() {
		if (mNextCastAt > mSimEngine.getSimTime()) return mNextCastAt - mSimEngine.getSimTime();

		return 0.0;	// XXX: No idea when the next cast is.
	}

	public double getTargetCastTime() {
		return mTargetCastTime;
	}

	public int getNrCasts() {
		return mNrCasts;
	}

	public int getNrInterruptedCasts() {
		return mNrInterruptedCasts;
	}
}
